package com.example.gulimall.cart.web.controller;

import com.example.common.vo.cart.CartItemVo;
import com.example.gulimall.cart.web.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zyred
 * @since v 0.1
 **/
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/cart.html")
    public String cartList (Model model) {
        return this.cartService.cartList (model);
    }


    @GetMapping("/addItem")
    public String addCartItem (@RequestParam("skuId") Long skuId,
                           @RequestParam("num") int num,
                           RedirectAttributes attributes) {

        attributes.addAttribute("skuId", skuId);
        return this.cartService.addItem(skuId, num);
    }

    @GetMapping("/getItem.html")
    public String getItem (@RequestParam("skuId") Long skuId, Model model) {
        return this.cartService.getItem(skuId, model);
    }

    @GetMapping("/checkItem")
    public String checkItem (@RequestParam("skuId") Long skuId,
                             @RequestParam("checked") boolean checked,
                             Model model) {
        return this.cartService.checkItem(skuId, checked, model);
    }

    @ResponseBody
    @GetMapping("/current/user/item/{userId}")
    public List<CartItemVo> getCurrentUserItem (@PathVariable("userId") Long userId) {
        return this.cartService.getCurrentUserItem(userId);
    }

}
